package com.sigmazero13.bgjeeves.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.text.SpannableString;
import android.text.util.Linkify;
import android.widget.TextView;

import com.sigmazero13.bgjeeves.BoardGameUtilsApp;
import com.sigmazero13.bgjeeves.R;

public class AboutDialogBuilder {
	public static AlertDialog create( Activity activity ) throws NameNotFoundException {
		// Try to load the a package matching the name of our own package
		PackageInfo pInfo = activity.getPackageManager().getPackageInfo(activity.getPackageName(), PackageManager.GET_META_DATA);
		String versionInfo = pInfo.versionName;

		String aboutTitle = String.format("About %s", activity.getString(R.string.app_name));
		String versionString = String.format("Version: %s", versionInfo);
		String aboutText = activity.getString(R.string.about_text);

		Integer dbVersion = ((BoardGameUtilsApp)activity.getApplication()).getDbHelper().getDbVersion();
		String dbVersionString = String.format("DB Version: %d", dbVersion);
		
		// Set up the TextView
		final TextView message = new TextView(activity);
		// We'll use a spannablestring to be able to make links clickable
		final SpannableString s = new SpannableString(aboutText);
		
		// Set some padding
		message.setPadding(5, 5, 5, 5);
		// Set up the final string
		message.setText(versionString + "\n\n" + s + "\n\n" + dbVersionString);
		// Now linkify the text
		Linkify.addLinks(message, Linkify.ALL);

		return new AlertDialog.Builder(activity).setTitle(aboutTitle).setCancelable(true).setIcon(R.drawable.icon).setPositiveButton(
			 activity.getString(android.R.string.ok), null).setView(message).create();
	}
}

